#!/bin/bash

#=======================================================================
# git-ignore
# File ID: e95d1ab0-30ff-11e4-a73e-fefdb24f8e10
#
# Add entries to the top-level .gitignore . Wildcards can be used, and 
# if any of the arguments is a directory, a trailing slash will be added 
# if it doesn't exist already.
#
# Syntax:
#   gitignore file [files]
#     Place files, symlinks or directories in a global .gitignore in the 
#     root directory.
#   gitignore -l file [files]
#     Use local .gitignore in the current directory.
#
# Author: Øyvind A. Holm <sunny@sunbase.org>
# License: GNU General Public License version 2 or later.
#=======================================================================

unset opt_local

if test "$1" = "-l"; then
    opt_local=1
    gitignore=.gitignore
    shift
else
    gitignore="$(git rev-parse --show-toplevel)/.gitignore"
fi

for f in "$@"; do
    unset termslash
    test -d "$f" && { echo "$f" | grep -q '/$' || termslash=/; }
    git ls-files -- "$f" | grep -q . && git rm -r --cached "$f"
    if test -n "$opt_local"; then
        # Use local .gitignore in current directory
        echo $f$termslash >>"$gitignore"
    else
        # Use global .gitignore at top of repo
        echo /$(git rev-parse --show-prefix)$f$termslash >>"$gitignore"
    fi
done

sort -u "$gitignore" >"$gitignore.tmp"
mv "$gitignore.tmp" "$gitignore"
git add "$gitignore"
